package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Brand;
import com.wy.bean.Car;
import com.wy.bean.Shop;
import com.wy.service.ShopService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class ShopController {

    @Resource
    private ShopService shopService;

    //进入首页
    @RequestMapping("head")
    public String head(){
        return "head";
    }
    //进入添加品牌
    @RequestMapping("brand")
    public String brand(Model model){
        model.addAttribute("dqpage","brand");
        return "brand";
    }
    //进入添加商品
    @RequestMapping("addShop")
    public String addShop(Model model){
        List<Brand> brandlist = shopService.getBrandlist();
        //查询到品牌列表，传递到前台
        model.addAttribute("brandlist",brandlist);
        model.addAttribute("dqpage","addShop");
        return "addShop";
    }
    //进入商品列表
    @RequestMapping("shoplist")
    public String shoplist(Model model, String bid, @RequestParam(defaultValue = "1")int pageNum
            , @RequestParam(defaultValue = "3")int pageSize){
        if (StringUtil.isNotEmpty(bid)) {//如果传入bid不等于null，根据bid查找商品列表
            PageHelper.startPage(pageNum,pageSize);
            List<Shop> shoplist = shopService.getShoplist(bid);
            PageInfo<Shop> pageInfo = new PageInfo<>(shoplist);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("bid",bid);//回显bid，说明当前查询的是哪一个
        }
        List<Brand> brandlist = shopService.getBrandlist();
        //查询到品牌列表，传递到前台
        model.addAttribute("brandlist",brandlist);
        model.addAttribute("dqpage","shoplist");
        return "shoplist";
    }
    //进入购物车
    @RequestMapping("mycar")
    public String mycar(Model model){
        List<Car> list = shopService.getList();
        model.addAttribute("list",list);
        model.addAttribute("dqpage","mycar");
        return "mycar";
    }
    //新增品牌
    @RequestMapping("saveBrand")
    public String saveBrand(Model model, Brand brand, MultipartFile file){
        try {
            if(file !=null && file.getSize()>0){
                String originalFilename = file.getOriginalFilename();
                String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
                String savename= UUID.randomUUID().toString()+substring;
                file.transferTo(new File("D:\\myupload\\"+savename));
                brand.setPic("/myupload/"+savename);
            }
            shopService.addBrand(brand);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:brand.do";
    }
    //新增商品
    @RequestMapping("saveShop")
    public String saeShop(Model model, Shop shop, MultipartFile file){
        try {
            if(file !=null && file.getSize()>0){
                String originalFilename = file.getOriginalFilename();
                String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
                String savename= UUID.randomUUID().toString()+substring;
                file.transferTo(new File("D:\\myupload\\"+savename));
                shop.setImg("/myupload/"+savename);
            }
            shopService.addShop(shop);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:addShop.do";
    }
    //加入购物车
    @RequestMapping("addCar")
    @ResponseBody
    public boolean addCar(String sid) {
        try {
            shopService.addCar(sid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
